﻿using AppForm_Managers.Object_Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppForm_Managers.Views
{
    public partial class Emp_Postions : Form
    {
        public Emp_Postions()
        {
            InitializeComponent();
        }

        private void Emp_PostionsQualifications_Load(object sender, EventArgs e)
        {
            UseComboProvinces_DataLoad();
            ComboxQualification_loadDtata();
            Controls_Reset();
            GridEmpPosQuas_LoadData();
        }

        private void UseComboProvinces_DataLoad()
        {
            try
            {
                UseComboProvinces.DataSource = null;
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    List<Province> list = new List<Province>();

                    Province temp = new Province()
                    {
                        ProvID = 0,
                        NameOfProvince = "Lựa chọn"
                    };

                    list.Add(temp);
                    list.AddRange(db.Provinces);

                    UseComboProvinces.DataSource = list;
                    UseComboProvinces.DisplayMember = "NameOfProvince";
                    UseComboProvinces.ValueMember = "ProvID";
                    UseComboProvinces.SelectedIndex = 0;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
        private void UseComboProvinces_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    District temp = new District()
                    {
                        DistID = 0,
                        NameOfDist = "Lựa chọn"
                    };

                    List<District> list = new List<District>();
                    list.Add(temp);

                    if (UseComboProvinces.SelectedIndex == 0)
                    {
                        UseComboDistricts.DataSource = list;
                        UseComboDistricts.ValueMember = "DistID";
                        UseComboDistricts.DisplayMember = "NameOfDist";
                        UseComboDistricts.SelectedIndex = 0;
                    }
                    else
                    {
                        int provinceID = int.Parse(UseComboProvinces.SelectedValue.ToString());
                        Province province = db.Provinces.SingleOrDefault(p => p.ProvID == provinceID);
                        list.AddRange(province.Districts.ToList());

                        UseComboDistricts.DataSource = list;
                        UseComboDistricts.ValueMember = "DistID";
                        UseComboDistricts.DisplayMember = "NameOfDist";
                        UseComboDistricts.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void UseComboDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    Commune temp = new Commune()
                    {
                        ComID = 0,
                        NameOfComm = "Lựa chọn"
                    };

                    List<Commune> list = new List<Commune>();
                    list.Add(temp);

                    if (UseComboDistricts.SelectedIndex == 0)
                    {
                        UseComboCommunes.DataSource = list;
                        UseComboCommunes.ValueMember = "ComID";
                        UseComboCommunes.DisplayMember = "NameOfComm";
                        UseComboCommunes.SelectedIndex = 0;
                    }
                    else
                    {
                        int districtID = int.Parse(UseComboDistricts.SelectedValue.ToString());
                        District district = db.Districts.SingleOrDefault(p => p.DistID == districtID);
                        list.AddRange(district.Communes.ToList());

                        UseComboCommunes.DataSource = list;
                        UseComboCommunes.ValueMember = "ComID";
                        UseComboCommunes.DisplayMember = "NameOfComm";
                        UseComboCommunes.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void UseComboCommunes_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    Organization temp = new Organization()
                    {
                        OrgID = 0,
                        NameOfOrgan = "Lựa chọn"
                    };

                    List<Organization> list = new List<Organization>();
                    list.Add(temp);

                    if (UseComboCommunes.SelectedIndex == 0)
                    {
                        UseComboOgrans.DataSource = list;
                        UseComboOgrans.ValueMember = "OrgID";
                        UseComboOgrans.DisplayMember = "NameOfOrgan";
                        UseComboOgrans.SelectedIndex = 0;
                    }
                    else
                    {
                        int communeID = int.Parse(UseComboCommunes.SelectedValue.ToString());
                        Commune commune = db.Communes.SingleOrDefault(p => p.ComID == communeID);
                        list.AddRange(commune.Organizations.ToList());

                        UseComboOgrans.DataSource = list;
                        UseComboOgrans.ValueMember = "OrgID";
                        UseComboOgrans.DisplayMember = "NameOfOrgan";
                        UseComboOgrans.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void UseComboOgrans_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    Department temp = new Department()
                    {
                        DepID = 0,
                        NameOfDep = "Lựa chọn"
                    };

                    List<Department> list = new List<Department>();
                    list.Add(temp);

                    if (UseComboOgrans.SelectedIndex == 0)
                    {
                        UseComboDeparts.DataSource = list;
                        UseComboDeparts.ValueMember = "DepID";
                        UseComboDeparts.DisplayMember = "NameOfDep";
                        UseComboDeparts.SelectedIndex = 0;
                    }
                    else
                    {
                        int organID = int.Parse(UseComboOgrans.SelectedValue.ToString());
                        Organization organ = db.Organizations.SingleOrDefault(p => p.OrgID == organID);
                        list.AddRange(organ.Departments.ToList());

                        UseComboDeparts.DataSource = list;
                        UseComboDeparts.ValueMember = "DepID";
                        UseComboDeparts.DisplayMember = "NameOfDep";
                        UseComboDeparts.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void UseComboDeparts_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    Employee temp = new Employee()
                    {
                        EmpID = 0,
                        FirstName = "Lựa chọn"
                    };

                    List<Employee> list = new List<Employee>();
                    list.Add(temp);

                    if (UseComboDeparts.SelectedIndex == 0)
                    {
                        ComboEmployee.DataSource = list;
                        ComboEmployee.ValueMember = "EmpID";
                        ComboEmployee.DisplayMember = "FirstName";
                        ComboEmployee.SelectedIndex = 0;
                    }
                    else
                    {
                        int departID = int.Parse(UseComboDeparts.SelectedValue.ToString());
                        Department emp = db.Departments.SingleOrDefault(p => p.DepID == departID);
                        list.AddRange(emp.Employees1.ToList());

                        ComboEmployee.DataSource = list;
                        ComboEmployee.ValueMember = "EmpID";
                        ComboEmployee.DisplayMember = "FirstName";
                        ComboEmployee.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void ComboEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //Controls_Reset();
                ErrorProvider.Dispose();
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    if (ComboEmployee.SelectedIndex == 0)
                    {
                        //GridEmpTrainning.DataSource = null;
                        ViewGroupBox.Visible = false;
                        AddButton.Enabled = false;
                    }
                    else
                    {
                        int empID = Int32.Parse(ComboEmployee.SelectedValue.ToString());
                        Employee emp = db.Employees.SingleOrDefault(p => p.EmpID == empID);
                        GridEmpPosQuas.DataSource = emp.EmployeesQualifications.ToList();
                        LableCodeEmp.Visible = true;
                        LableCodeEmp.Text = emp.Code;
                        LableFullName.Visible = true;
                        LableFullName.Text = String.Format("{0} {1}", emp.FirstName, emp.LastName);
                        Genderlabel.Visible = true;

                        if (emp.Gender == true)
                            Genderlabel.Text = "Nam";
                        else
                            Genderlabel.Text = "Nữ";

                        LableEmail.Text = emp.Email;
                        LablePhone.Text = emp.Telephone;
                        string avatarUrl = String.Format(@"{0}Uploads\{1}", Ultis_Code.FindSystemUrl.getDefaultURL(), emp.AvatarUrl);
                        Bitmap bm = new Bitmap(avatarUrl);
                        PictureBoxAvata.Image = bm;
                        PictureBoxAvata.SizeMode = PictureBoxSizeMode.StretchImage;
                        ViewGroupBox.Visible = true;
                        AddButton.Enabled = true;
                        //GridEmpTrainning.DataSource = emp.TrainingFosterings.ToList();



                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void CheckCurrentDateQua_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckCurrentDateQua.Checked)
            {
                DateQualification.Text = DateTime.Now.ToString("MM/dd/yyyy");
                DateQualification.Enabled = false;
            }
            else
            {
                DateQualification.Enabled = true;
            }
        }

        private void ComboxQualification_loadDtata()
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    Qualification temp = new Qualification()
                    {
                        QuaID = 0,
                        NameOfQua = "Lựa chọn"
                    };

                    List<Qualification> list = new List<Qualification>();
                    list.Add(temp);
                    list.AddRange(db.Qualifications.ToList());
                    ComboxQualification.DataSource = list;

                    ComboxQualification.ValueMember = "QuaID";
                    ComboxQualification.DisplayMember = "NameOfQua";

                    ComboxQualification.SelectedIndex = 0;

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
        private void Controls_Reset()
        {
            AddButton.Enabled = false;
            UpdateButton.Enabled = false;

            ComboxQualification.SelectedIndex = 0;

            DateQualification.Text = "";
            CheckCurrentDateQua.Checked = false;
            CheckStatusQua.Checked = false;
        }

        private void CancelsButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn hủy bỏ và làm mới", "Hủy bỏ và làm mới", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Controls_Reset();
                UseComboProvinces.SelectedIndex = 0;
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            ErrorProvider.Dispose();

            if (ComboxQualification.SelectedIndex == 0)
            {
                ErrorProvider.SetError(ComboxQualification, "Hãy chọn vị trình độ chuyên môn");
                ComboxQualification.Focus();
                return;
            }
            else
                ErrorProvider.Dispose();

            if (DateQualification.Text == "")
            {
                ErrorProvider.SetError(DateQualification, "Hãy nhập ngày bắt đầu nhận trình độ chuyên môn");
                DateQualification.Focus();
                return;
            }
            else
                ErrorProvider.Dispose();

            if (DateTime.Parse(DateQualification.Text) > DateTime.Now)
            {
                ErrorProvider.SetError(DateQualification, "Ngày tháng không hợp lệ");
                DateQualification.Focus();
                
                return;
            }
            else
                ErrorProvider.Dispose();
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    int empId = Int32.Parse(ComboEmployee.SelectedValue.ToString());
                    Employee em = db.Employees.Single(p => p.EmpID == empId);
                    EmployeesQualification empQua = new EmployeesQualification()
                    {
                        EmpID = empId,
                        QuaID = Int32.Parse(ComboxQualification.SelectedValue.ToString()),
                        StartOfDate = DateTime.Parse(DateQualification.Text),
                        StatusOfEmpQua = CheckStatusQua.Checked
                    };

                    db.EmployeesQualifications.Add(empQua);
                    db.SaveChanges();
                    Emp_PostionsQualifications_Load(sender, e);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            
                
        }

        private void GridEmpPosQuas_LoadData()
        {
            try
            {
                using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
                {
                    int empID = Int32.Parse(ComboEmployee.SelectedValue.ToString());
                    Employee emp = db.Employees.SingleOrDefault(p => p.EmpID == empID);
                    
                    
                    
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Hệ thống đang bận, hãy thử lại sau.", "Hệ thống thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

    }
}